package com.loe.httptest

import android.app.Activity
import android.view.Gravity
import android.view.ViewGroup
import com.loe.mvp.BaseDialog
import com.loe.mvp.ext_view.setDelayClickListener
import com.loe.mvp.ext_app.showSoft
import com.loe.mvp.ext_java.delay
import com.loe.mvp.ext_view.isEmpty
import com.loe.mvp.ext_view.string
import kotlinx.android.synthetic.main.http_dialog_parse.view.*

class HttpParseDialog(activity: Activity) : BaseDialog(activity, R.layout.http_dialog_parse)
{
    private var on: ((s: String) -> Unit)? = null

    override fun ViewGroup.onView()
    {
        setAnimation()
        setCancelAfterDismiss()

        buttonCancel.setOnClickListener()
        {
            cancel()
        }

        buttonOk.setDelayClickListener()
        {
            if (editText.isEmpty)
            {
                toast(editText.hint.toString())
                return@setDelayClickListener
            }

            on?.invoke(editText.string)
            cancel()
        }

        delay(200)
        {
            activity.showSoft(editText)
        }
    }

    fun show(on: (s: String) -> Unit)
    {
        this.on = on
        super.show(Gravity.CENTER)
    }
}